<template>
	<view class="main-container" ref="mainRef">
		<!-- 头部表单 -->
		<view class="form-main" ref="formRef">
			<myFrom :form="form" ref="myFromRef" :formList="formList" labelWidth="130" input-height="56"></myFrom>
		</view>
		<!-- 添加物料查询 -->
		<view class="search-form" ref="searchRef" v-if="needEdit">
			<view class="search-form-item">
				<view style="max-width: 40%;min-width: 35%;display: flex;">
					<u-input placeholder="图号" v-model="searchForm.Code" border height="56"></u-input>
				</view>
				<view style="max-width: 40%;min-width: 35%;display: flex;">
					<u-input placeholder="物料名称" v-model="searchForm.MaterialName" border height="56"></u-input>
				</view>
				<u-button type="primary" @click="searchGoods" size="mini" style="margin-right: 10rpx;">查询</u-button>
			</view>
		</view>
		<!-- 表格 -->
		<view class="add-data-part" :style="{height:remainingHeight}">
			<view class="add-data-th">
				<u-table font-size="20" v-if="!maskConfig.show" style="height: auto;z-index: 100;">
					<u-tr>
						<u-th width="8%">
							<u-checkbox v-model="selectAllData1" @click.native="selectAll(1)"></u-checkbox>
						</u-th>
						<u-th width="30%">图号</u-th>
						<u-th width="35%">物料名称</u-th>
						<u-th width="12%">数量</u-th>
						<!-- <u-th width="calc( (100% - 46rpx) * 15% )">金额</u-th> -->
						<u-th width="15%">储位</u-th>
						<!-- <u-th width="15%">操作</u-th> -->
					</u-tr>
				</u-table>
			</view>
			<view class="add-data-td">
				<scroll-view class="" scroll-y="true" style="height: 100%;width: 100%;" :enhanced="true"
					:scroll-with-animation="true">
					<u-table font-size="20" style="height: auto;z-index: 100;" v-if="!maskConfig.show"
						class="table-data-part">
						<template v-for="(row,rowIndex) in tableDatas.datas">
							<u-tr>
								<u-td width="8%">
									<u-checkbox v-model="row.isChecked"></u-checkbox>
								</u-td>
								<u-td width="30%" class="first-list">
									{{row.Code}}
								</u-td>
								<u-td width="35%" class="second-list">
									{{row.MaterialName}}
								</u-td>
								<u-td width="12%" class="third-list">
									<u-input v-model="row.ReceiptQty" :placeholder="needEdit?'输入数量':''"
										type="number"></u-input>
								</u-td>
								<u-td width="15%">
									{{ row.ShelvesIDlabel 	 }}
								</u-td>
							</u-tr>
						</template>
					</u-table>
				</scroll-view>
			</view>
		</view>
		<!-- 添加物料弹窗 -->
		<view class="myMask" v-if="maskConfig.show">
			<view class="mask-body">
				<view class="mask-body__title">
					<h3>添加物料</h3>
				</view>
				<view class="table-part-1">
					<u-table font-size="20">
						<u-tr>
							<u-th width="10%">
								<u-checkbox v-model="selectAllData" @click.native="selectAll(0)"></u-checkbox>
							</u-th>
							<u-th width="15%">类型</u-th>
							<u-th width="30%">图号</u-th>
							<u-th width="45%">品名</u-th>
						</u-tr>
					</u-table>
				</view>
				<view class="table-part">
					<scroll-view scroll-y="true" style="height: 100%;width: 100%;" :enhanced="true"
						:scroll-with-animation="true" @scrolltolower="scrolltolower()" :lower-threshold="10">
						<u-table font-size="20" style="height: auto;">
							<template v-for="(row,rowIndex) in maskConfig.visibleData">
								<u-tr>
									<u-td width="10%" style="height: 100%; line-height: 25.6px;">
										<u-checkbox v-model="row.isChecked"></u-checkbox>
									</u-td>
									<u-td width="15%">
										{{row.MaterialType}}
									</u-td>
									<u-td width="30%" class="td-wrap">
										{{row.Code}}
									</u-td>
									<u-td width="45%" class="td-wrap">
										{{row.MaterialName}}
									</u-td>

								</u-tr>
							</template>
							<u-loadmore :status="maskConfig.loadmoreVisible" :lpad-text="maskConfig.loadmoreText"
								@loadmore="loadMore()" color="#3e82f9" />
						</u-table>
					</scroll-view>
				</view>
				<view class="button-part">
					<u-button type='success' @click="addNewMaterial" v-if="hasPermission">新增物料</u-button>
					<u-button type='error' @click="cancalAdd">取 消</u-button>
					<u-button type="primary" @click="confirmAdd" v-if="hasPermission">确定添加</u-button>
				</view>
			</view>
		</view>
		<!-- 新增物料弹窗 -->
		<!-- <view class="myMask1" v-if="maskConfig.addNew">
			<view class="mask-body">

			</view>
		</view> -->
		<!-- 底部按钮 -->
		<view class="fixed-bottom">
			<template v-if="needEdit">
				<u-button class="batch-button" type="warning" @click="batchEdit()" v-if="hasPermission">改储</u-button>
				<u-button class="add-button" type="error" border @click="deleteRow()" v-if="hasPermission">删除</u-button>
				<u-button class="batch-button" type="success" @click="saveDraft()" v-if="hasPermission">保存</u-button>
				<u-button class="add-button" type="primary" @click="showConfirm()" v-if="hasPermission">提交</u-button>
			</template>


		</view>
		<!-- 选择储位 -->
		<u-select v-model="selectShow" :list="WarehouseOptions" @confirm="confirm($event)"></u-select>
		<!-- 创建成功提示 -->
		<u-modal v-model="confirmShow" :content="confrimContent" mode="center" @confirm="modalConfirm"></u-modal>
	</view>
</template>

<script>
	import myFrom from '@/components/myForm/index.vue'
	export default {
		components: {
			myFrom
		},

		data() {
			const date = this.$u.timeFormat(new Date, 'yyyy-mm-dd')
			return {
				tableDatas: {
					dicID: 1204,
					datas: [],
					columns: [],
					formData: {},
					loading: false,
				},
				searchForm: {
					Code: '',
					MaterialName: '',
					MaterialType: '标准件,非标件'
				},
				form: {},
				formList: [{
						prop: 'ReceiptDate',
						type: 'date',
						label: '单据日期',
						placeholder: '请选择单据日期',
						Required: true,
						ControlType: 'datebox',
						value: date
					},
					{
						prop: 'ReceiptID',
						type: 'input',
						label: '单据单号',
						placeholder: '不填则自动编码',
						Required: false,
						ControlType: 'textbox',
						value: ''
					},
					{
						prop: 'ReceiptType',
						type: 'select',
						label: '单据类型',
						items: [{
								label: '采购入库',
								value: '采购入库'
							},
							{
								label: '生产入库',
								value: '生产入库'
							},
							{
								label: '销售退货',
								value: '销售退货'
							},
							{
								label: '还回入库',
								value: '还回入库'
							},
							{
								label: '期初入库',
								value: '期初入库'
							},
							{
								label: '调拨入库',
								value: '调拨入库'
							},
							{
								label: '盘盈',
								value: '盘盈'
							},
						],
						show: false,
						Required: true,
						ControlType: 'combobox',
						value: '采购入库',
						placeholder: '选择类型',
					},
					{
						prop: 'reconciliation',
						type: 'cascader',
						label: '往来单位',
						items: [{
								value: 'CustomerID',
								label: '客户',
								children: [],
							},
							{
								value: 'SupplierID',
								label: '供应商',
								children: [],
							},
						],
						show: false,
						Required: false,
						ControlType: 'mutilCombobox',
						placeholder: '选择往来单位',
					},
					{
						prop: 'ReceiptMan',
						type: 'input',
						label: '交货人',
						Required: false,
						ControlType: 'textbox',
						placeholder: '填写交货人',
					},
					{
						prop: 'Remark1',
						type: 'input',
						label: '单据备注',
						placeholder: '填写备注',
						Required: false,
						ControlType: 'textbox	'
					},
				],
				rules: {
					ReceiptDate: [{
						required: true,
						message: '必填',
						// blur和change事件触发检验
						trigger: ['blur'],
					}],
					ReceiptType: [{
						required: true,
						message: '必填',
						// blur和change事件触发检验
						trigger: ['blur'],
					}],
					ReceiptID: [{
						asyncValidator: (rule, value, callback) => {
							if (!value) return callback()
							if (this.isEdit) return callback()
							this.$request.post({
								url: '/APSAPI/APSData',
								data: {
									ReceiptID: value,
									dicID: 3006
								}
							}).then(res => {
								if (!res.result) {
									callback(new Error('校验出错'));
								} else if (res.data.length) {
									// 如果校验通过，也要执行callback()回调
									callback(new Error('单号已存在'));
								} else {
									callback();
								}
							}).catch(() => {
								callback(new Error('校验出错'));
							})
						},
					}],
				},
				maskConfig: {
					show: false,
					loading: false,
					datas: [],
					columns: [],
					page: 1,
					rows: 50, // 每次加载的数据量
					visibleData: [],
					loadmoreVisible: 'loadmore',
					loadmoreText: '滑动或点击加载更多',
					addNew: false,
					form: {},
					formList: [{
							prop: 'MaterialType',
							type: 'select',
							label: '类型',
							placeholder: '请选择类型',
							Required: true,
							items: [{
									value: '标准件',
									label: '标准件'
								},
								{
									value: '非标件',
									label: '非标件'
								},
							],
							show: false,
							ControlType: 'combobox',
							value: '标准件'
						},
						{
							prop: 'Code',
							type: 'input',
							label: '图号',
							placeholder: '请输入图号',
							Required: true,
							ControlType: 'textbox',
							value: ''
						}, {
							prop: 'MaterialName',
							type: 'input',
							label: '品名',
							placeholder: '请输入品名',
							Required: true,
							ControlType: 'textbox',
							value: ''
						}, {
							prop: 'Unit',
							type: 'input',
							label: '单位',
							placeholder: '请输入单位',
							Required: false,
							ControlType: 'textbox',
							value: ''
						}, {
							prop: 'DefaultShelvesID',
							type: 'select',
							label: '默认储位',
							placeholder: '请选择储位',
							Required: false,
							items: [, ],
							show: false,
							ControlType: 'combobox',
							value: ''
						},
					]
				},
				selectAllData: false,
				selectAllData1: false,
				WarehouseOptions: [],
				selectShow: false,
				selectRow: {},
				confirmShow: false,
				remainingHeight: '50vh',
				confrimContent: '成功创建入库单！',
				rowDatas: {},
				needEdit: false,
				isEdit: false,
				hasPermission: false,
			}
		},
		watch: {
			rowDatas: {
				handler: function(newVal) {
					this.$nextTick(() => {
						this.calculateRemainingHeight()
					})
					if (!this.isEdit) return this.clearForm()
					this.formList.forEach((val) => {
						if (val.prop === 'reconciliation') {
							this.form.CustomerID = this.rowDatas.CustomerID
							this.form.SupplierID = this.rowDatas.SupplierID
						} else {
							this.form[val.prop] = this.rowDatas[val.prop] || null;
							this.form[val.prop + 'label'] = this.rowDatas[val.prop] || null;
							if (val.prop === 'ReceiptID') {
								this.$set(val, 'disabled', true)
							}
						}
					});
					this.queryAddedGoods()
				},
				// immediate: true
			}
		},
		onLoad(options) {
			const resulut = this.$store.getters.userInfo.RoleMap?.some(val => val.RoleID === 'E01Admin' || val.RoleID ===
				'R2309260003') // 管理员和仓管人员
			this.$set(this, 'hasPermission', resulut)
			const data = JSON.parse(options.maskConfig)
			this.isEdit = data.isEdit
			this.rowDatas = data.rowDatas
			this.needEdit = data.needEdit
			this.clearForm()
			this.initData()
		},
		created() {
			// this.clearForm()
			// this.initData()
		},
		mounted() {
			this.$nextTick(() => {
				this.$refs.myFromRef?.$refs.uFormRef?.setRules(this.rules);
				this.calculateRemainingHeight()
			})
		},
		methods: {
			// 获取数据
			async getApsData(remark = 0) {
				const condition = {
					dicID: this.tableDatas.dicID,
				}
				Object.assign(condition, this.tableDatas.formData)
				this.tableDatas.loading = true
				const res = await this.$request.post({
					url: '/APSAPI/APSData',
					data: condition
				}).catch(err => err)
				this.tableDatas.loading = false
				if (!res) return
				const {
					result,
					msg,
					data,
					Columns
				} = res
				if (!result) return uni.showToast({
					title: msg,
					icon: 'error'
				})
				this.$set(this.tableDatas, 'datas', data)
				this.$set(this.tableDatas, 'columns', Columns[0])
			},
			// 获取表头
			async getApsConfig() {
				const condition = [{
					ID: this.tableDatas.dicID,
				}]
				const res = await this.$request.post({
					url: '/APSAPI/GetConfig',
					data: condition
				}).catch(err => err)
				if (!res) return
				const {
					result,
					msg,
					datas,
				} = res
				if (!result) return uni.showToast({
					title: msg,
					icon: 'error'
				})
				this.$set(this.tableDatas, 'columns', datas[0])
			},
			// 获取储位
			async getWarehouseData() {
				const condition = {
					dicID: 24411
				}
				const res = await this.$request.post({
					url: '/APSAPI/APSData',
					data: condition
				}).catch(err => err)
				if (res.result) {
					this.WarehouseOptions = res.data.map((item) => ({
						label: item.ShelvesCode,
						value: item.ShelvesID,
						WarehouseName: item.WarehouseName,
						RackCode: item.RackCode,
					}));
				}
			},
			// 获取往来单位
			async getCustomerData() {
				// 获取供应商
				const res = await this.$request.post({
					url: '/APSAPI/APSData',
					data: {
						dicID: 32
					}
				}).catch(err => err)
				if (res.result) {
					const targetobj = this.formList.find(
						(val) => val.prop === 'reconciliation',
					);
					targetobj.items[0].children = res.data.map((item) => {
						if (this.form.CustomerID) this.$set(this.form, 'reconciliationlabel', '客户/' + item
							.CustomerName)
						return {
							label: item.CustomerName,
							value: item.CustomerID,
						}
					});
				}
				// 获取客户
				const supplierRes = await this.$request.post({
					url: '/APSAPI/APSData',
					data: {
						dicID: 1198
					}
				}).catch(err => err)
				if (supplierRes.result) {
					const targetobj = this.formList.find(
						(val) => val.prop === 'reconciliation',
					);
					targetobj.items[1].children = supplierRes.data.map((item) => {
						if (this.form.SupplierID) this.$set(this.form, 'reconciliationlabel', '供应商/' + item
							.SupplierName)
						return {
							label: item.SupplierName,
							value: item.SupplierID,
						}
					});
				}

			},
			// 初始化数据
			initData() {
				this.getWarehouseData() // 获取储位
				this.getCustomerData() // 获取客户信息
				this.getApsConfig() // 获取表头
			},
			// 查询货品
			async searchGoods() {
				this.maskConfig.show = true
				this.$set(this.maskConfig, 'datas', [])
				this.$set(this.maskConfig, 'show', true)
				this.setLoadmore('loading')
				const data = {
					dicID: 1180,
					page: this.maskConfig.page,
					rows: this.maskConfig.rows
				}
				Object.assign(data, this.searchForm)
				const res = await this.$request.post({
					url: '/APSAPI/APSData',
					data: data
				}).catch(err => err)
				if (res.result) {
					this.$set(this.maskConfig, 'datas', res.data)
					if (!res.data.length) {
						this.setLoadmore('nomore')
					} else {
						this.maskConfig.visibleData.push(...this.maskConfig.datas)
						this.setLoadmore('loadmore')
					}
				} else {
					return Promise.reject()
				}
			},
			// 选择全部
			selectAll(index) {
				if (index === 0) {
					this.maskConfig.visibleData.forEach(val => {
						this.$set(val, 'isChecked', this.selectAllData)
					})
				} else if (index === 1) {
					this.tableDatas.datas.forEach(val => {
						this.$set(val, 'isChecked', this.selectAllData1)
					})
				}

			},
			// 确定添加
			confirmAdd() {
				const selectData = this.maskConfig.visibleData.filter(val => val.isChecked)
				if (!selectData || !selectData.length) return uni.showToast({
					title: '请选择数据',
					icon: 'error'
				})
				const data = this.tableDatas.datas
				const filterData = selectData.filter(val => {
					return data.every(element => element.MaterialID !== val.MaterialID)
				})
				filterData.forEach(row => {
					row['update'] = true;
					row['ReceiptDetailID'] = '';
					row['ReceiptQty'] = null;
					row['Price'] = null;
					row['ShelvesIDlabel'] = null;
					row['Status'] = 1;
					row['show'] = false
					row['所有储位'] = this.WarehouseOptions;
					row.isChecked = false
					if (row['DefaultShelvesID']) {
						//如果有默认储位，直接配置给对应储位信息
						row['ShelvesID'] = row['DefaultShelvesID'];
						const selectOption = this.WarehouseOptions.find(
							(val) => val.value === row['DefaultShelvesID'],
						);
						if (selectOption) {
							this.$set(row, 'ShelvesIDlabel', selectOption.label);
							this.$set(row, 'WarehouseName', selectOption.WarehouseName);
							this.$set(row, 'RackCode', selectOption.RackCode);
							this.$set(row, 'DefaultShelvesCode', selectOption.label);
						}
					}
					data.push(row)
				})
				this.maskConfig.visibleData.length = 0
				this.maskConfig.page = 1
				this.setLoadmore()
				this.maskConfig.show = false
			},
			//取消添加
			cancalAdd() {
				this.maskConfig.visibleData.length = 0
				this.maskConfig.page = 1
				this.setLoadmore()
				this.maskConfig.show = false
			},
			// 删除行
			deleteRow(index = null) {
				// this.tableDatas.datas?.splice(index, 1)
				const length = this.tableDatas.datas?.length
				if (!length) return
				for (let i = length - 1; i >= 0; i--) {
					if (this.tableDatas.datas[i].isChecked) {
						this.tableDatas.datas?.splice(i, 1)
					}
				}
			},
			// 打开下拉选择储位
			openSelect(row) {
				// this.selectShow = true
				this.$set(this, 'selectShow', true)
				this.selectRow = row
			},
			// 批量修改储位
			batchEdit() {
				this.$set(this, 'selectShow', true)
				this.selectRow = null
			},
			// 确定选择储位
			confirm(e) {
				if (this.selectRow) {
					this.selectRow.ShelvesID = e[0].value
					this.$set(this.selectRow, 'ShelvesIDlabel', e[0].label)
					if (e[0]?.value) {
						const selectOption = this.WarehouseOptions.find(
							(val) => val.value === e[0]?.value,
						);
						if (selectOption) {
							this.$set(this.selectRow, 'WarehouseName', selectOption.WarehouseName);
							this.$set(this.selectRow, 'RackCode', selectOption.RackCode);
						}
					}
				} else {
					const editData = this.tableDatas.datas.filter(val => val.isChecked)
					editData.forEach(val => {
						val.ShelvesID = e[0].value
						this.$set(val, 'ShelvesIDlabel', e[0].label)
						if (e[0]?.value) {
							const selectOption = this.WarehouseOptions.find(
								(element) => element.value === e[0]?.value,
							);
							if (selectOption) {
								this.$set(val, 'WarehouseName', selectOption.WarehouseName);
								this.$set(val, 'RackCode', selectOption.RackCode);
							}
						}
					})
				}

			},
			// 提交前确认
			async showConfirm() {
				const uFormRef = this.$refs.myFromRef?.$refs.uFormRef
				const validate = await uFormRef?.validate().catch(err => err)
				if (!validate) return
				const submitData = [];
				const obj = JSON.parse(JSON.stringify(this.form))
				const childrens = JSON.parse(JSON.stringify(this.tableDatas.datas))
				let msg = ''
				if (!childrens.length) msg = '请添加货品'
				if (msg) return uni.showToast({
					title: msg,
					icon: 'error'
				})
				obj.dicID = 3006;
				obj.OperationStatus = 2; // 草稿0 提交2
				childrens.forEach((val) => {
					val.dicID = 1204;
					for (let key in this.form) {
						val[key] = val[key] || this.form[key];
					}
				});
				let isRequired = this.isRequiredData(
					this.tableDatas.columns,
					childrens,
				);
				if (isRequired) {
					uni.showModal({
						content: `数据第${isRequired.index}行必填项【${isRequired.col.label}】为空或为0`,
						showCancel: false,
						confirmText: '确定'
					});
					return
				}
				obj.childrens = childrens;
				submitData.push(obj);
				uni.showModal({
					title: '提示',
					content: '确认提交数据吗？',
					success: (res) => {
						if (res.confirm) {
							// 用户点击确认
							this.submitData(submitData);
						} else if (res.cancel) {
							// 用户点击取消
							uni.showToast({
								title: '已取消',
								icon: 'none'
							});
						}
					}
				});
			},
			// 提交入库 
			async submitData(submitData) {
				const res = await this.$request.post({
					url: '/APSAPI/SaveData',
					data: submitData
				}).catch(err => err)
				if (res.result) {
					this.confrimContent = '入库单已提交!'
					this.$set(this, 'confirmShow', true)
				} else {
					uni.showToast({
						title: res.msg,
						icon: 'error'
					})
				}
			},
			// 校验必填项 是否已填写
			isRequiredData(column, rows) {
				let requiredArr = column.filter((val) => {
					return val.Required;
				});
				let errorColIndex;
				if (!requiredArr || !requiredArr.length) return false;
				let newData = requiredArr.find((val) => {
					return !rows.every((value, index) => {
						if (value[val.prop] && value[val.prop] != '0') {
							return true;
						} else {
							errorColIndex = index + 1;
							return false;
						}
					});
				});
				if (!newData) return false;
				else return {
					col: newData,
					index: errorColIndex
				};
			},
			// 确认弹窗点击确认
			confirmCilck() {
				this.$set(this, 'confirmShow', false)
			},
			// 表格滚动到底部
			scrolltolower() {
				this.loadMore()
			},
			//加载更多
			loadMore() {
				if (this.maskConfig.loadmoreVisible === 'nomore') return
				this.maskConfig.page++
				this.searchGoods()
			},
			//配置加载更多
			setLoadmore(status = 'loadmore') {
				const textMenu = {
					loadmore: '滑动或点击加载更多',
					loading: '加载中...',
					nomore: '已无更多数据',
				}
				this.$set(this.maskConfig, 'loadmoreVisible', status)
				this.$set(this.maskConfig, 'loadmoreText', textMenu[status])
			},
			//校验订单是否存在
			async checkReceiptID() {
				let data = this.form.ReceiptID
				let msg = ''
				if (!data) return msg
				const res = await this.$request.post({
					url: '/APSAPI/APSData',
					data: {
						dicID: 3006,
						ReceiptID: data,
					}
				}).catch(err => err)
				if (!res.result) {
					msg = '校验出错：' + res.msg
				} else {
					if (res.data.length) msg = '单号已存在'
				}
				return msg
			},
			// 重置表单和清空选择物料
			clearForm() {
				if (!this.isEdit) {
					this.formList.forEach(val => {
						this.$set(this.form, val.prop, val.value || null)
						this.$set(this.form, val.prop + 'label', val.value || null)
					})
					// 多级选择器 清空配置字段
					this.form.CustomerID = null;
					this.form.SupplierID = null;
					this.$set(this.tableDatas, 'datas', [])
				}

			},
			//
			modalConfirm() {
				this.clearForm()
				this.$u.route({
					type: 'back',
					url: '/pages/Isgo/inbound/inboundList',
				});
			},
			// 计算高度
			calculateRemainingHeight() {
				// 获取 main-container 的高度
				const query = uni.createSelectorQuery().in(this);
				query.select('.main-container').boundingClientRect();
				query.select('.form-main').boundingClientRect();
				query.select('.search-form').boundingClientRect();
				query.select('.fixed-bottom').boundingClientRect();
				query.exec((rects) => {
					const mainHeight = rects[0]?.height || 0;
					const formHeight = rects[1]?.height || 0;
					const searchHeight = rects[2]?.height || 0;
					const bottomHeight = rects[3]?.height || 0;
					// 计算剩余高度 = 容器高度 - 表单高度 - 搜索高度 - 其他可能需要减去的间距
					this.remainingHeight = mainHeight - formHeight - searchHeight - bottomHeight + 'px'
				});
			},
			// 粘贴数据处理
			async pasteDescription(e) {
				// 阻止默认粘贴行为（可选）
				e.preventDefault();
				// #ifdef H5
				// 浏览器环境下直接获取剪贴板数据
				try {
					// 检查浏览器是否支持 Clipboard API
					if (navigator.clipboard && navigator.clipboard.readText) {
						const pastedText = await navigator.clipboard.readText();
						this.processData(pastedText)
					} else {
						// 降级方案：用 setTimeout 获取输入框变化后的值
						setTimeout(() => {
							this.processData(e.target.value)
						}, 50);
					}
				} catch (err) {
					// 降级处理
					setTimeout(() => {
						this.processData(e.target.value);
					}, 50);
				}
				// #endif

				// #ifdef APP
				// App 端需要调用 uni.getClipboardData()
				uni.getClipboardData({
					success: (res) => {
						this.processData(res.data);
						// pastedText = res.data
					},
					fail: (err) => {
						console.log("获取剪贴板失败", err);
					}
				});
				// #endif

			},
			// 粘贴数据处理
			processData(pastedText) {
				let str = pastedText.trim();
				// 检查是否包含换行符
				if (str.includes('\n')) {
					let arr = str.split('\n');
					str = arr.join(',');
				}
				if (str.includes('\ ')) {
					let arr = str.split('\ ').filter((val) => val);
					str = arr.join(',');
				}
				setTimeout(() => {
					this.$set(this.searchForm, 'Code', str);
				}, 200);
			},
			// 新增物料
			addNewMaterial() {
				uni.showToast({
					title: '功能暂未开放',
					icon: 'error'
				})
			},
			//保存草稿
			async saveDraft() {
				const formRef = this.$refs.myFromRef?.$refs.uFormRef
				if (formRef) {
					const validate = await formRef.validate().catch(err => err)
					if (!validate) return
				}
				const submitData = [];
				const obj = JSON.parse(JSON.stringify(this.form))
				const childrens = JSON.parse(JSON.stringify(this.tableDatas.datas))
				obj.dicID = 3006;
				obj.OperationStatus = 0; // 草稿0 提交2
				childrens.forEach((val) => {
					val.dicID = 1204;
					for (let key in this.form) {
						val[key] = val[key] || this.form[key];
					}
				});
				if (this.isEdit) {
					const deleteData = this.tableDatas.metadata.filter((val) => {
						return !childrens.find((value) => value.MaterialID == val.MaterialID);
					});
					if (deleteData.length) {
						deleteData.forEach((val) => (val.ElementDeleteFlag = 1));
						childrens.push(...deleteData);
					}
				}
				obj.childrens = childrens;
				submitData.push(obj);
				const res = await this.$request.post({
					url: '/APSAPI/SaveData',
					data: submitData
				}).catch(err => err)
				if (res.result) {
					this.confrimContent = '保存草稿成功！'
					this.$set(this, 'confirmShow', true)
					// this.$emit('closeMask')
				} else {
					uni.showToast({
						title: res.msg,
						icon: 'error'
					})
				}
			},
			// 查看订单 获取已添加物料信息
			async queryAddedGoods() {
				const condition = {
					dicID: 1204,
					ReceiptID: this.rowDatas.ReceiptID,
					DataType: '入库',
				}
				this.tableDatas.loading = true
				const res = await this.$request.post({
					url: '/APSAPI/APSData',
					data: condition
				}).catch(err => err)
				this.tableDatas.loading = false
				if (!res) return
				const {
					result,
					msg,
					data,
					Columns
				} = res
				if (!result) return uni.showToast({
					title: msg,
					icon: 'error'
				})
				data.forEach(val => {
					if (val.ShelvesID) {
						const item = this.WarehouseOptions.find(element => element
							.value === val.ShelvesID)
						this.$set(val, 'ShelvesIDlabel', item?.label || '')
					}
				})
				this.$set(this.tableDatas, 'datas', data)
				this.$set(this.tableDatas, 'metadata', JSON.parse(JSON.stringify(data)))
			},
		}

	}
</script>

<style scoped lang="scss">
	.main-container {
		width: 100vw;
		height: 100vh;
		overflow: hidden;
		display: flex;
		flex-direction: column;
		position: relative;

		.form-main {
			width: 100%;
			padding: 20rpx 20rpx 10rpx 30rpx;
			border-bottom: 1px solid $uni-border-color;
		}

		.fixed-bottom {
			border-top: 1px solid $uni-border-color;
			background-color: $uni-bg-color;
			padding: 20rpx 0;
			z-index: 99;
			display: flex;
			justify-content: space-around;
			align-items: center;

			.batch-button,
			.add-button {
				width: 20% !important;
				border-radius: 50rpx;
				height: 80rpx !important;
			}


		}

	}


	.search-form {
		width: 100%;
		padding: 20rpx 30rpx;

		.search-form-item {
			display: flex;
			align-items: center;
			justify-content: space-around;

			.u-input {
				width: 35%;
			}

			.u-button {
				width: 20%;
			}
		}

	}

	.mask-body {
		width: 80vw;
		height: 90%;
		overflow: hidden;
		display: flex;
		flex-direction: column;

		.mask-body__title {
			background-color: $uni-bg-color;
			padding: 10rpx 0;
			text-align: center;
		}

		.table-part {
			width: 100%;
			flex-grow: 1;
			overflow: hidden;
			background-color: $uni-bg-color;
			padding: 0 8rpx 10rpx;

		}

		.button-part {
			padding: 10rpx 0 20rpx 0;
			background-color: $uni-bg-color;
			width: 100%;
			// height: 120rpx;
			display: flex;

			.u-button {
				width: 35%;
			}
		}

		.td-wrap {
			text-wrap: wrap !important;
		}

		.u-td {
			height: 60rpx;
		}
	}

	.u-table {
		// -webkit-overflow-scrolling: touch;
		// overflow: hidden ;
		// height: 100%;
		width: 100%;
		overflow-y: auto;
		overflow-x: hidden;

		::v-deep .u-input__input {
			font-size: small !important;
		}
	}

	.u-td,
	.u-th {
		white-space: nowrap !important;
		overflow: hidden !important;
		text-overflow: ellipsis !important;
		min-width: 0 !important;
		/* 关键属性 */
		flex-shrink: 0 !important;
		/* 防止flex容器压缩 */

		.u-input {
			height: 66rpx;
		}
	}

	scroll-view {
		-webkit-overflow-scrolling: touch;
	}

	.add-data-part {
		width: 100%;
		flex-grow: 1;
		overflow: hidden !important;
		background-color: $uni-bg-color;
		min-height: 0;
		display: flex;
		flex-direction: column;
		// padding-bottom: 131rpx;

		.add-data-th {
			width: 100%;
		}

		.add-data-td {
			flex-grow: 1;
			width: 100%;
			min-height: 0;
			overflow: hidden;
		}

		.u-td {
			height: 80rpx;
		}

		.u-th {
			z-index: 99;
		}
	}

	.myMask {
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		z-index: 1000;
		background-color: rgba(0, 0, 0, 0.5);
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.first-list {
		font-weight: bold !important;
		font-size: 24rpx !important;
		text-wrap: wrap !important;
	}

	.second-list {
		white-space: pre-wrap !important;
		font-size: 18rpx !important;
	}

	.third-list {
		background-color: #ffff7f;

		.u-input {
			text-indent: 5rpx;
		}
	}
</style>